/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ContextualRenderedImageFactory;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.CollectionImage;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import ca.forklabs.media.jai.operator.PipelineDescriptor;
import ca.forklabs.media.jai.FormatDataType;
import ca.forklabs.media.jai.ParameterBlockUtil;
import ca.forklabs.media.jai.SpectralFilter2D;
import ca.forklabs.media.jai.operator.SpectralHomomorphicDescriptor;

/**
 * Class {@code SpectralFilterCRIF} is a
 * {@link ContextualRenderedImageFactory} supporting the
 * <em>spectralfilter</em> operation in the rendered and renderable image
 * layers.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.operator.SpectralFilterCRIF">Daniel Léonard</a>
 * @version $Revision$
 */
public class SpectralFilterCRIF extends AbstractCRIF {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public SpectralFilterCRIF() {
      super(SpectralHomomorphicDescriptor.NAME);
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the spectral filter.
    * @param   pb   the parameter block.
    * @return   the spectral filter.
    */
   protected SpectralFilter2D getFilter(ParameterBlock pb) {
      int index = SpectralHomomorphicDescriptor.FILTER_PARAMETER_INDEX;
      SpectralFilter2D filter = (SpectralFilter2D) pb.getObjectParameter(index);
      return filter;
      }

   /**
    * Gets the type for the format operation.
    * @param   pb   the parameter block.
    * @return   type for the format operation.
    */
   protected int getDataType(ParameterBlock pb) {
      int index = SpectralHomomorphicDescriptor.TYPE_PARAMETER_INDEX;
      FormatDataType format = (FormatDataType) pb.getObjectParameter(index);
      int type = format.getDataBufferType();
      return type;
      }

   /**
    * Builds the centered filter image.
    * @param   filter   the filter.
    * @param   width   the desired width.
    * @param   height   the desired height.
    * @param   elements   the number of elements, half the number of bands.
    * @param   hints   the rendering hints.
    * @return   the filter image.
    */
   protected RenderedImage getRawFilter(SpectralFilter2D filter, int width, int height, int elements, RenderingHints hints) {
      filter.setWidth(width);
      filter.setHeight(height);
      RenderedImage sink = filter.getFilterImage(elements, hints);
      return sink;
      }

   /**
    * Does a periodic shift of half the image size in both dimensions.
    * @param source   the image to shift.
    * @param hints   the rendering hints.
    * @return   the shifted image.
    */
   @SuppressWarnings("nls")
   protected RenderedImage shift(RenderedImage source, RenderingHints hints) {
      ParameterBlock pb = ParameterBlockUtil.createPeriodicShiftParameterBlock(source);
      RenderedImage sink = JAI.create("periodicshift", pb, hints);
      return sink;
      }

   /**
    * Filters the Fourier image.
    * @param   source   the Fourier image.
    * @param   filter   the filter.
    * @param   hints   the rendering hints.
    * @return   the spectral image filtered.
    */
   @SuppressWarnings("nls")
   protected RenderedImage filter(RenderedOp source, SpectralFilter2D filter, RenderingHints hints) {
      int width = source.getWidth();
      int height = source.getHeight();
      int elements = source.getNumBands() / 2;   // the image is complex, it is a FFT after all

      RenderedImage source0 = this.shift(source, hints);
      RenderedImage source1 = this.getRawFilter(filter, width, height, elements, hints);

      ParameterBlock pb = ParameterBlockUtil.createMultiplyComplexParameterBlock(source0, source1);
      RenderedImage filtered = JAI.create("multiplycomplex", pb, hints);

      RenderedImage sink = this.shift(filtered, hints);

      return sink;
      }


//---------------------------
// Implemented methods from javax.media.jai.CRIFImpl
//---------------------------

   /**
    * Enhance the image.
    * @param   pb   the parameter block.
    * @param   hints   optional rendering hints.
    */
   @Override
   @SuppressWarnings("nls")
   public RenderedImage create(ParameterBlock pb, RenderingHints hints) {
      CollectionImage sources = (CollectionImage) pb.getSource(0);
      RenderedImage source = (RenderedImage) sources.get(0);

      SpectralFilter2D filter = this.getFilter(pb);
      int type = this.getDataType(pb);

      String[] pre_operations = new String[] {
         "format",
         "dft",
         };
      ParameterBlock[] pre_pbs = new ParameterBlock[] {
         ParameterBlockUtil.createFormatParameterBlock(null, type),
         ParameterBlockUtil.createDFTParameterBlock(null),
         };
      RenderedOp fft = PipelineDescriptor.create(source, pre_operations, pre_pbs, hints);

   // before doing the filtering, one could ask why the periodic shifting
   // is not done int the pre and post pipeline. the shifting parameter
   // block factory needs an image to calculates the default shifting
   // parameter. the shifting is done in the filter method.
      RenderedImage cooked = this.filter(fft, filter, hints);

      String[] post_operations = new String[] {
         "idft",
         };
      ParameterBlock[] post_pbs = new ParameterBlock[] {
         ParameterBlockUtil.createIDFTParameterBlock(null),
         };
      RenderedImage sink = PipelineDescriptor.create(cooked, post_operations, post_pbs, hints);
      return sink;
      }

// TODO : is the create method for renderable necessary ?

   }
